package unittestcase;

import android.test.ActivityInstrumentationTestCase2;
import android.app.Activity;
import junit.framework.AssertionFailedError;

import com.bitbar.recorder.extensions.ExtSolo;

public class Testcase4 extends ActivityInstrumentationTestCase2<Activity> {

	private static final String LAUNCHER_ACTIVITY_CLASSNAME = "com.gorillalogic.monkeytalk.demo1.RootActivity";
	private static Class<?> launchActivityClass;
	static {
		try {
			launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
	private ExtSolo solo; // ExtSolo is an extension of Robotium Solo that helps collecting better test execution data during test runs

	@SuppressWarnings("unchecked")
	public Testcase4() {
		super((Class<Activity>) launchActivityClass);
	}

	@Override
	public void setUp() throws Exception {
		super.setUp();
		solo = new ExtSolo(getInstrumentation(), getActivity(), this.getClass()
				.getCanonicalName(), getName());
	}

	@Override
	public void tearDown() throws Exception {
		solo.finishOpenedActivities();
		solo.tearDown();
		super.tearDown();
	}

	public void testRecorded() throws Exception {
		try {
			solo.waitForActivity("RootActivity");
			solo.waitForActivity("LoginActivity");
			solo.sleep(3000);
			
			solo.clickOnView(14);
			solo.waitForActivity("WebActivity");
			solo.sleep(3800);
			assertTrue(
					"Wait for html element - input(index: 0, id: userInput) failed.",
					solo.waitForHtmlElement("input", "userInput", "", "", 0,
							20000, null));
			solo.clickOnHtmlElement("input", "userInput", "", "", 0, null);
			solo.sleep(4300);
			assertTrue(
					"Wait for html element - input(index: 0, id: userInput) failed.",
					solo.waitForHtmlElement("input", "userInput", "", "", 0,
							20000, null));
			solo.enterTextIntoHtmlElement("input", "userInput", "", "", "He",
					0, null);
			assertTrue(
					"Wait for html element - textarea(index: 0, id: txtarea) failed.",
					solo.waitForHtmlElement("textarea", "txtarea", "", "", 0,
							20000, null));
			solo.clickOnHtmlElement("textarea", "txtarea", "", "", 0, null);
			solo.sleep(1400);
			assertTrue(
					"Wait for html element - textarea(index: 0, id: txtarea) failed.",
					solo.waitForHtmlElement("textarea", "txtarea", "", "", 0,
							20000, null));
			solo.clickOnHtmlElement("textarea", "txtarea", "", "", 0, null);
			solo.sleep(2600);
			assertTrue(
					"Wait for html element - textarea(index: 0, id: txtarea) failed.",
					solo.waitForHtmlElement("textarea", "txtarea", "", "", 0,
							20000, null));
			solo.enterTextIntoHtmlElement("textarea", "txtarea", "", "",
					"This is a textarea 2", 0, null);
			assertTrue(
					"Wait for html element - select(index: 0, id: Fruits, name: Fruits) failed.",
					solo.waitForHtmlElement("select", "Fruits", "Fruits", "",
							0, 20000, null));
			solo.clickOnHtmlElement("select", "Fruits", "Fruits", "", 0, null);
			solo.sleep(1400);
			assertTrue("Wait for list (index: 0) failed.",
					solo.waitForList(0, 20000));
			solo.clickInList(4, 0);
			solo.sleep(2000);
			assertTrue(
					"Wait for html element - input(index: 0, id: b, name: rbox) failed.",
					solo.waitForHtmlElement("input", "b", "rbox", "", 0, 20000,
							null));
			solo.clickOnHtmlElement("input", "b", "rbox", "", 0, null);
			solo.sleep(1000);
			assertTrue(
					"Wait for html element - input(index: 0, id: c, name: rbox) failed.",
					solo.waitForHtmlElement("input", "c", "rbox", "", 0, 20000,
							null));
			solo.clickOnHtmlElement("input", "c", "rbox", "", 0, null);
			solo.sleep(1400);
			assertTrue(
					"Wait for html element - input(index: 0, id: e, name: cbox) failed.",
					solo.waitForHtmlElement("input", "e", "cbox", "", 0, 20000,
							null));
			solo.clickOnHtmlElement("input", "e", "cbox", "", 0, null);
			solo.sleep(1400);
			assertTrue(
					"Wait for html element - input(index: 0, name: btn) failed.",
					solo.waitForHtmlElement("input", "", "btn", "", 0, 20000,
							null));
			solo.clickOnHtmlElement("input", "", "btn", "", 0, null);
		} catch (AssertionFailedError e) {
			solo.fail(
					"com.gorillalogic.monkeytalk.demo1.Testcase3.testRecorded_scr_fail",
					e);
			throw e;
		} catch (Exception e) {
			solo.fail(
					"com.gorillalogic.monkeytalk.demo1.Testcase3.testRecorded_scr_fail",
					e);
			throw e;
		}
	}

}
